package com.feihong.txgw.tx_precenter;

import com.feihong.txgw.inter.contract.kaika.IKaikaSelectPhoneNumContract;
import com.feihong.txgw.json.kaika.Menu;
import com.feihong.txgw.json.kaika.PhoneNumList;
import com.feihong.txgw.tx_model.KaikaSelectPhoneNumMainModel;

import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName KaikaSelectPhoneNumMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/4 15:42
 * @Describe
 * @Change
 */
public class KaikaSelectPhoneNumMainPrecenter extends IKaikaSelectPhoneNumContract.AbKakaSelectPhoneNumMainPrecenter {

    private int mCurrentPageNum = 1 ;
    private boolean isFullPage = true;

    public static KaikaSelectPhoneNumMainPrecenter create(){
        return new KaikaSelectPhoneNumMainPrecenter();
    }
    @Override
    public void loadPhoneNumMenu(String iccid) {
        mIModel.getPhoneNumMenu(iccid).subscribe(new Observer<Menu>() {
            @Override
            public void onSubscribe(Disposable d) {

                mRxManager.register(d);
            }

            @Override
            public void onNext(Menu menu) {
                mIView.loadPhoneNumMenuResult(menu);
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadPhoneNumMenuError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void loadPhoneNumList(String iccid, final String phoneNum, final String packageID, Map<String,String> params) {
        int pageNum = mCurrentPageNum;
        mIModel.getPhoneNumList(iccid,String.valueOf(pageNum),phoneNum,packageID,params).subscribe(new Observer<PhoneNumList>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(PhoneNumList phoneNumList) {
                if(phoneNumList.getCode() == 200 && phoneNumList.getData() != null){
                    if(isFullPage && mCurrentPageNum <= 1){
                        isFullPage = phoneNumList.getData().size() > 6;
                    }
                    mCurrentPageNum++;
                    mIView.loadPhoneNumListResult(isFullPage,phoneNumList.getData());
                }else{
                    mIView.loadPhoneNumEnd();
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mIView.loadPhoneNumListError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void loadPhoneNumListFirst(String iccid, String phoneNum, String packageID, Map<String, String> params) {
        mCurrentPageNum = 1;
        isFullPage = true;
        loadPhoneNumList(iccid,phoneNum,packageID,params);
    }

    @Override
    public IKaikaSelectPhoneNumContract.IKaikaSelectPhoneNumMainModel getModel() {
        return KaikaSelectPhoneNumMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
